package com.yf.aidlcaller;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;

import com.yf.aidlservice.IService;
import com.yf.aidlservice.IService.Stub;


public class MainActivity extends Activity {

    private MyServiceConnection conn;
    private IService iService;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent service = new Intent();
        service.setAction("com.yf.remotemethod");
        conn = new MyServiceConnection();
        bindService(service, conn, BIND_AUTO_CREATE);
    }
    
    private class MyServiceConnection implements ServiceConnection {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			iService = Stub.asInterface(service);
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			Log.v("AIDLCaller.MainActivity", "onServiceDisconnected");
		}
    	
    }
    
    public void callRemoteMethod(View v) {
    	try {
			iService.remoteMethod();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
    }
    
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	unbindService(conn);
    }
}
